using micromsg;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Storage;
using System;
using System.IO;
namespace MicroMsg.Scene
{
	public class NetSceneDownLoadPackage : NetSceneBaseEx<DownLoadPackageRequest, DownLoadPackageResponse, DownLoadPackageRequest.Builder>
	{
		private const string TAG = "NetSceneDownLoadPackage";
		public const int DOSCENE_SUCCESS = 1;
		public const int DOSCENE_SYS_ERR = 0;
		public const int DOSCENE_LOADING_ERR = -1;
		private uint mOffset;
		private uint mGetLen;
		private uint mTotalLen;
		private int mTimesToServer;
		private PacageData mPackage;
		private MemoryStream mDownloadStream;
		public bool doScene(PacageData package)
		{
			this.mPackage = package;
			this.mOffset = 0u;
			this.mGetLen = 102400u;
			this.mTimesToServer = 0;
			return this.doScene();
		}
		private bool doScene()
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.Offset = this.mOffset;
			this.mBuilder.Len = this.mGetLen;
			this.mBuilder.Type = (uint)this.mPackage.nType;
			Package.Builder builder = new Package.Builder();
			builder.Name = this.mPackage.Name;
			builder.PackName = this.mPackage.strPackName;
			builder.Version = this.mPackage.nVersion;
			builder.Id = this.mPackage.nId;
			this.mBuilder.Package = builder.Build();
			this.mSessionPack.mCmdUri = "/cgi-bin/micromsg-bin/downloadpackage";
			this.mSessionPack.mConnectMode = 2;
			base.endBuilder();
			this.mTimesToServer++;
			return true;
		}
		protected override void onFailed(DownLoadPackageRequest request, DownLoadPackageResponse response)
		{
			Log.smethod_1("NetSceneDownLoadPackage", "system error DownLoad Package failed !");
			EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOADPACKAGE_ERR, null, null);
		}
		protected override void onSuccess(DownLoadPackageRequest request, DownLoadPackageResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				Log.smethod_1("NetSceneDownLoadPackage", "Download Package failed because of net error");
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOADPACKAGE_ERR, null, null);
				return;
			}
			if (response.TotalSize > 0u && this.mTotalLen <= 0u)
			{
				this.mTotalLen = response.TotalSize;
			}
			else
			{
				if (this.mTimesToServer >= 10)
				{
					Log.smethod_1("NetSceneDownLoadPackage", "failed because Too many times to Download Package ");
					EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOADPACKAGE_ERR, null, null);
					return;
				}
			}
			if (response.PackageBuf.ILen > 0u)
			{
				if (this.mDownloadStream == null)
				{
					this.mDownloadStream = new MemoryStream();
				}
				int num = response.PackageBuf.Buffer.ToByteArray().Length;
				if (num <= 0)
				{
					Log.smethod_1("NetSceneDownLoadPackage", "Get Package lenght is 0");
					return;
				}
				NetSceneDownLoadPackage.writeBufToStream(response.PackageBuf.Buffer.ToByteArray(), this.mOffset, (uint)num, this.mDownloadStream);
				this.mOffset += (uint)num;
			}
			if (!this.isDownloadComplete())
			{
				this.doScene();
				return;
			}
			string zipFileName = PackageDataMgr.getZipFileName(this.mPackage);
			StorageIO.writeToFile(zipFileName, this.mDownloadStream.ToArray(), false);
			string packBigFileName = PackageDataMgr.getPackBigFileName(this.mPackage);
			string strKeyword = (this.mPackage.nId > 9) ? ("ChatBackground_" + this.mPackage.nId + "@2x.jpg") : ("ChatBackground_0" + this.mPackage.nId + "@2x.jpg");
			MemoryStream imgStream = PackageDataMgr.zipParas(zipFileName, strKeyword);
			if (PackageDataMgr.saveBigImg(imgStream, packBigFileName))
			{
				EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOADPACKAGE_SUCCESS, new object[]
				{
					packBigFileName,
					this.mPackage.nId
				}, null);
				StorageIO.deleteFile(zipFileName);
				return;
			}
			EventCenter.postEvent(EventConst.ON_NETSCENE_DOWNLOADPACKAGE_ERR, null, null);
		}
		public bool isDownloadComplete()
		{
			return this.mOffset >= this.mTotalLen && this.mTotalLen > 0u;
		}
		private static void writeBufToStream(byte[] byte_0, uint startPos, uint ndataLen, MemoryStream mStream)
		{
			if (byte_0 != null && mStream != null)
			{
				try
				{
					mStream.Seek((long)((ulong)startPos), 0);
					mStream.Write(byte_0, 0, (int)ndataLen);
				}
				catch (Exception ex)
				{
					Log.smethod_1("NetSceneDownLoadPackage", ex.get_Message());
				}
				return;
			}
		}
	}
}
